import { AuthState } from '@/redux/interface';
import * as types from '@/redux/mutation-types';
import { Draft, produce } from 'immer';
import { Action } from 'redux';

const authRouter: AuthState = {
  authRouter: [],
  authButton: []
};

interface SET_AUTH_ROUTER extends Action {
  type: typeof types.SET_AUTH_ROUTER;
  authRouter: [];
}

interface SET_AUTH_BUTTON extends Action {
  type: typeof types.SET_AUTH_BUTTON;
  authButton: [];
}

type ActionType = SET_AUTH_ROUTER | SET_AUTH_BUTTON;

const auth = (state: AuthState = authRouter, action: ActionType) => {
  // produce处理嵌套对象
  return produce(state, (draftState: Draft<AuthState>) => {
    switch (action.type) {
      case types.SET_AUTH_ROUTER:
        draftState.authRouter = action.authRouter;
        break;
      case types.SET_AUTH_BUTTON:
        draftState.authButton = action.authButton;
        break;
      default:
        break;
    }
  });
};

export default auth;
